<script setup lang="ts">
import { loginByPassword, loginMessage, loginMessagenumber } from '@/services/user';
import cp from '../../components/cp-nav-bar.vue'
import { ref } from 'vue';
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
// import { defineStore } from 'pinia'
import { useUserStore } from '@/stores';
import type { User } from '@/types/user';
// import { argumentPlaceholder } from '@babel/types';
import { showToast } from 'vant'
// import router from '@/router';
import { useRouter } from 'vue-router';
const router = useRouter()
const mobile = ref('13230000001')
const password = ref('abc12345')
const code = ref('')
const agree = ref('')
const btn = async () => {
  if (!agree.value) {
    return showToast('请勾选同意')
  }
  const res = pass.value ? await loginByPassword(mobile.value, password.value) : await loginMessagenumber(mobile.value, code.value)
  console.log(res.data
  );

  const store = useUserStore()

  store.setUser(res.data)
  router.push('/')

}
const second = ref(0);
const send = async () => {
  // 倒计时
  // 初始化值
  const res = await loginMessage(mobile.value, 'login')
  console.log(res);
  second.value = 10
  var timer: number
  // 倒计时
  timer = window.setInterval(function () {

    second.value--
    if (second.value <= 0) {
      window.clearInterval(timer)
    }
  }, 1000)



}
const pass = ref(true)

</script>

<template>
  <div class="login-page">
    <cp right-text="注册" @click-right="$router.push('/register')"></cp>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ pass?'密码登录': '短信登录' }}</h3>
      <a href="javascript:;" @click="pass = !pass">
        <span>{{ pass?'密码登录': '短信登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off">
      <van-field placeholder="请输入手机号" type="tel" v-model="mobile" :rules="mobileRules"></van-field>
      <van-field v-if="pass" placeholder="请输入密码" type="password" v-model="password" :rules="passwordRules"></van-field>
      <van-field v-else placeholder="请输入验证码" v-model="code" :rules="codeRules">
        <template #button>
          <span class="btn-send" :class="{ active: second > 0 }" @click="send">
            {{ second > 0 ? `${second}s后再次发送` : '发送验证码' }}
          </span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" @click="btn">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }

  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;

    h3 {
      font-weight: normal;
      font-size: 24px;
    }

    a {
      font-size: 15px;
    }
  }

  &-other {
    margin-top: 60px;
    padding: 0 30px;

    .icon {
      display: flex;
      justify-content: center;

      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;

  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;

    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }

  .btn-send {
    color: var(--cp-primary);

    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>







